package com.bxait.toymail.member.controller;

import java.util.Arrays;
import java.util.Map;


import com.bxait.com.exception.BizCodeEnum;
import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.R;
import com.bxait.com.vo.member.SocialUser;
import com.bxait.com.vo.member.UserLoginVo;
import com.bxait.com.vo.member.UserRegisterVo;
import com.bxait.toymail.member.exception.PhoneExistException;
import com.bxait.toymail.member.exception.UsernameExistException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.bxait.toymail.member.entity.MemberEntity;
import com.bxait.toymail.member.service.MemberService;

import javax.annotation.Resource;


/**
 * import org.apache.shiro.authz.annotation.RequiresPermissions;
 * 会员
 *
 * @author chen
 * @email 2277411582@qq.com
 * @date 2023-03-13 22:35:49
 */
@RestController
@RequestMapping("member/member")
public class MemberController {


    @Resource
    @Qualifier("memberService")
    private MemberService memberService;




    /*
     * @param socialUser
     * @==============History===============<br/>
     * @Description //   处理社交登录,注册登录二合一
     * @Date 2023/4/21 2023/4/21
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return
     */
    @RequestMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUser socialUser){
        MemberEntity entity =  memberService.oauthLogin(socialUser);
        if(entity!=null){
            return R.ok().setData(entity);
        }else{
            return R.error(BizCodeEnum.LOGIN_ACCOUNT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGIN_ACCOUNT_PASSWORD_EXCEPTION.getMsg());
        }
    }


    /*
     * @param vo
     * @==============History===============<br/>
     * @Description //   处理用户注册的请求
     * @Date 2023/4/21 2023/4/21
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return
     */
    @RequestMapping("/register")
    public R register(@RequestBody UserRegisterVo vo){
        try{
            memberService.register(vo);
        }catch (PhoneExistException e){
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(),BizCodeEnum.PHONE_EXIST_EXCEPTION.getMsg());
        }catch (UsernameExistException e){
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(),BizCodeEnum.USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }


    /*
     * @param vo
     * @==============History===============<br/>
     * @Description //   验证用户信息,处理简单登录的请求
     * @Date 2023/4/21 2023/4/21
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return
     */
    @RequestMapping("/login")
    public R login(@RequestBody UserLoginVo vo){
        MemberEntity memberEntity = memberService.login(vo);
        if(memberEntity != null){
            return R.ok().setData(memberEntity);
        }else{
            return R.error(BizCodeEnum.LOGIN_ACCOUNT_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.LOGIN_ACCOUNT_PASSWORD_EXCEPTION.getMsg());
        }
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);
        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
